package com.lbj.googleplay.googleplay100.ui.fragment;

import android.graphics.Color;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.lbj.googleplay.googleplay100.domain.AppInfo;
import com.lbj.googleplay.googleplay100.http.protocol.RecommendProtocol;
import com.lbj.googleplay.googleplay100.ui.fly.ShakeListener;
import com.lbj.googleplay.googleplay100.ui.fly.StellarMap;
import com.lbj.googleplay.googleplay100.ui.view.LoadingPage;
import com.lbj.googleplay.googleplay100.utils.LogUtils;
import com.lbj.googleplay.googleplay100.utils.UIUtils;

import java.util.ArrayList;
import java.util.Random;

/*
* 推荐
* */
public class RecommendFragment extends BaseFragment {

    private ArrayList<String> data;

    @Override
    public View onCreateSuccessView() {
        final StellarMap stellarMap = new StellarMap(UIUtils.getContext());
        stellarMap.setAdapter(new RecommendAdapter());
        //设置随机方式,将控件划分为9行6列的格子,然后在格子中随机展示
        stellarMap.setRegularity(6,9);
        int padding = UIUtils.dip2px(10);
        //设置内边距
        stellarMap.setInnerPadding(padding,padding,padding,padding);
        //设置默认页面,默认显示第一组
        stellarMap.setGroup(0,true);
        //振动事件的处理
        ShakeListener listener = new ShakeListener(UIUtils.getContext());
        listener.setOnShakeListener(new ShakeListener.OnShakeListener() {
            @Override
            public void onShake() {
                stellarMap.zoomIn();//跳转到下一页数据
            }
        });

        return stellarMap;
    }

    @Override
    public LoadingPage.ResultState onLoad() {
        RecommendProtocol protocol = new RecommendProtocol();
        data = protocol.getData(0);
        return check(data);
    }

    class RecommendAdapter implements StellarMap.Adapter {
        //返回组的个数
        @Override
        public int getGroupCount() {
            return 2;
        }

        //返回某组的item个数
        @Override
        public int getCount(int group) {

            int count = data.size() / getGroupCount();

            if (group==getGroupCount()-1){
                //如果是最后一组,count值将可能会除不尽,要把余数加上
                count+=data.size()%getGroupCount();
            }

            return count;
        }
        //初始化布局
        @Override
        public View getView(int group, int position, View convertView) {
            //因为position每次都是从0开始,所以需要把前面页的item总数加上
            position += (group)*getCount(group-1);

            final String keyword = data.get(position);
            TextView view = new TextView(UIUtils.getContext());
            view.setText(keyword);
            //生成随机字号16-25之间
            Random random = new Random();
            int size = 16+random.nextInt(10);
            //设置字号,类型sp
            view.setTextSize(TypedValue.COMPLEX_UNIT_SP,size);
            //随机颜色 30-230之间
            int r = random.nextInt(200)+30;
            int g = random.nextInt(200)+30;
            int b = random.nextInt(200)+30;
            view.setTextColor(Color.rgb(r,g,b));
            //设置点击事件
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(UIUtils.getContext(), keyword, Toast.LENGTH_SHORT).show();
                }
            });
            return view;
        }
        //返回下一组的id
        @Override
        public int getNextGroupOnZoom(int group, boolean isZoomIn) {
            LogUtils.d("isZoomIn:=="+isZoomIn);

            if (isZoomIn){//在往下滑,加载上一页
                if (group>0){
                    group--;
                }else {//跳到最后一页
                    group = getGroupCount()-1;
                }
            }else {//在往上滑,加载下一页
                if (group<getGroupCount()-1){
                    group++;
                }else {
                    group=0;
                }
            }
            return group;
        }
    }
}
